<?php
/**
* 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Model_ETL
 * @copyright  Copyright (c) 2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Row.php 21 2009-11-15 02:12:58Z nkeyes $
 */

/**
 * 
 * @category   Bushido
 * @package    Bushido_ETL
 * @copyright  Copyright (c) 2009 Nathan Keyes
 */

class Bushido_Db_Table_Row extends Zend_Db_Table_Row_Abstract implements Zend_Acl_Resource_Interface
{
	
	/**
	 * (non-PHPdoc)
	 * @see library/Zend/Acl/Resource/Zend_Acl_Resource_Interface#getResourceId()
	 */
	public function getResourceId()
	{
		return $this->getTableClass();
	}
	
	
	/**
	 * 
	 * @return int
	 */
	public function getCreatorId()
	{
		
		$adapter = Bushido_Db_Table::getAdapter();
		$select = $adapter->select()
		->from('audit_log', 'bushido_user_id')
			->where('table_name = ?', $this->getTable()->info(Bushido_Db_Table_Abstract::NAME))
			->where('type = ?', 'insert')
			->where('pk = ?', $this->getPKString())
			->order('timestamp ASC')
			->limit(1);
			
		//Zend_Debug::dump("$select", '$select', true);exit;
		$bushido_user_id = $adapter->fetchOne($select);
		return $bushido_user_id;
	}
	
	/**
	 * 
	 * @return Bushido_DbTable_User
	 */
	public function getCreator()
	{
		$bushido_user_id = $this->getCreatorId();
		return Bushido_Db_Table::getTable('Bushido_DbTable_User')->find($bushido_user_id)->current();
	}
	
	/**
	 * 
	 * @return Bushido_DbTable_User
	 */
	public function getLastUpdatedBy()
	{
		$auditLogTable = Bushido_Db_Table::getTable('Bushido_DbTable_AuditLog');
		$auditLog = $auditLogTable->fetchAll
		(
			$auditLogTable->select()
			->where('table_name = ?', $this->getTable()->info(Bushido_Db_Table_Abstract::NAME))
			->where('pk = ?', $this->getPKString())
			->order('timestamp DESC')
			->limit(1)
		)
		->current();
		if($auditLog)
		{
			return $auditLog->findParentBushido_DbTable_User();
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getLastUpdatedDate()
	{
		$auditLogTable = Bushido_Db_Table::getTable('Bushido_DbTable_AuditLog');
		$auditLog = $auditLogTable->fetchAll
		(
			$auditLogTable->select()
			->where('table_name = ?', $this->getTable()->info(Bushido_Db_Table_Abstract::NAME))
			->where('pk = ?', $this->getPKString())
			->order('timestamp DESC')
			->limit(1)
		)
		->current();
		if($auditLog)
		{
			return $auditLog->timestamp;
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * 
	 * @return array
	 */
	public function getPKFields()
	{
		return $this->getTable()->info(Bushido_Db_Table_Abstract::PRIMARY);
	}
	
	/**
	 * 
	 * @return array
	 */
	public function getPK()
	{
		$pk = array();
		foreach($this->getPKFields() as $pkField)
		{
			$pk[$pkField] = $this->$pkField;
		}
		
		return $pk;
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getPKString()
	{
		return self::makePKString($this->getPK());
	}
	
	/**
	 * 
	 * @param $context
	 * @return string
	 */
	public function getDisplayValue($context = null)
	{
		return Bushido_Db_Table::getDisplayValue($this, $context);
	}
	
	/**
	 * 
	 * @return Bushido_Db_Table_Abstract
	 */
	public function getTable()
	{
		return parent::getTable();
	}
	
	/**
	 * 
	 * @return Bushido_Db_Form
	 */
	public function getForm()
	{
		return $this->getTable()->getForm($this->getPK());
	}
	
	
	/**
	 * 
	 * @param $overrides
	 * @return Bushido_Db_Table_Row
	 */
	public function duplicate(array $overrides = null)
	{
		return $this->getTable()->duplicate($this, $overrides);
	}
	
	public static function makePKString($fields)
	{
		$pkString = array();
		foreach($fields as $field => $value)
		{
			$pkString[] = "$field='$value'";
		}
		
		return implode(',', $pkString);
	}

}